package com.imc.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang.Validate;

/**
 * A role determines what action a person can perform on the system.
 */
@Entity
@Table(name = "roles")
public class Role implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * The length in characters of the role name.
	 */
	private static final int ROLE_NAME_LENGTH = 50;

	/**
	 * The id of the role.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id", nullable = false)
	private long id = 0;

	/**
	 * The name of the role.
	 */
	@Column(name = "name", nullable = false, length = ROLE_NAME_LENGTH)
	private String name;

	/**
	 * The default constructor.
	 * 
	 * Builds an empty role.
	 */
	protected Role() {
	}

	/**
	 * Creates an instance of Role.
	 * 
	 * @param theName
	 *            The name of the role. It cannot be null.
	 */
	public Role(final String name) {
		Validate.notNull(name, "The role name cannot be null.");
		this.name = name;
	}

	/**
	 * Modifies the values of the role.
	 * 
	 * @param name
	 *            The name of the role. It cannot be null.
	 */
	public void setName(final String name) {
		Validate.notNull(name, "The new role name cannot be null.");
		this.name = name;
	}

	/**
	 * Returns the name of the role.
	 * 
	 * @return the name, never returns null.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the id of the role.
	 * 
	 * @return Returns the role id.
	 */
	public long getId() {
		return id;
	}

	/**
	 * Indicates whether some other object is "equal to" this one.
	 * 
	 * @param obj
	 *            the reference object with which to compare.
	 * @return <code>true</code> if this object is the same as the object
	 */
	@Override
	public boolean equals(final Object obj) {
		if (!(obj instanceof Role)) {
			return false;
		}
		Role other = (Role) obj;
		return (other.name.equals(name) && (other.id == id));
	}

	/**
	 * Returns a hash code value for the object.
	 * 
	 * @return a hash code value for the object.
	 */
	@Override
	public int hashCode() {
		return name.hashCode();
	}

	/**
	 * Returns a string representation of the object.
	 * 
	 * @return a string representation of the object.
	 */
	@Override
	public String toString() {
		return "Role: " + name;
	}
}
